package com.deyuanyun.pic.app.controller;

import com.deyuanyun.pic.common.util.ImageUtil;
import com.deyuanyun.pic.common.util.UploadUtil;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
public class ReadController {
	@RequestMapping("/comm/image/{name}")
    public void image(@PathVariable String name,
    				   HttpServletRequest request,
    				   HttpServletResponse response) throws Exception {

		response.setContentType("image/jpeg");
		int width=0;
		int height=0;
		String widthStr=request.getParameter("w");
		String heightStr=request.getParameter("h");
		String id=request.getParameter("id");
		if(null != widthStr && !"".equals(widthStr))
		{
			width=Integer.parseInt(widthStr);
		}
		if(null != heightStr && !"".equals(heightStr))
		{
			height=Integer.parseInt(heightStr);
		}
		StringBuffer path=request.getRequestURL();
		String fileName=path.substring(path.indexOf(name));
		System.out.println(UploadUtil.ROOTPATH+UploadUtil.IMGRES+id+fileName.substring(fileName.indexOf("."),fileName.lastIndexOf(".")));
		ImageUtil.OuputImage(UploadUtil.ROOTPATH+UploadUtil.IMGRES+id+fileName.substring(fileName.indexOf("."),fileName.lastIndexOf(".")),width,height,response.getOutputStream());
}
}
